package com.hollycrm.unicom.sequenceservice.hibernate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.springframework.util.StringUtils;
import com.hollycrm.unicom.sequenceservice.SequenceManager;
import com.hollycrm.unicom.sequenceservice.SequenceObject;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class HibernateSequenceManager
    extends HibernateDaoSupport
    implements SequenceManager {

    protected final static SimpleDateFormat sdf = new SimpleDateFormat(
        "yyyyMMdd");
    protected final static String INIT_INDEX = "00001";

    public HibernateSequenceManager() {
    }

    /**
     * nextSequence
     *
     * @return String
     */
    public String nextSequence(final String seqID) {
        HibernateTemplate ht = getHibernateTemplate();
        return (String) ht.execute(new HibernateCallback() {
            /**
             * doInHibernate
             *
             * @param session Session
             * @return Object
             */
            public Object doInHibernate(Session session) throws
                HibernateException {
                List seqList = session.find("from o in " +
                                            SequenceObject.class +
                                            " where o.seqID = ?",
                                            seqID, Hibernate.STRING);
                SequenceObject sequenceObject = null;
                if (!seqList.isEmpty()) {
                    sequenceObject = (SequenceObject) seqList.get(0);
                }

                String today = sdf.format(new Date());

                if (sequenceObject != null) {
                    String seqDay = StringUtils.trim(sequenceObject.getToday());
                    String sequence = StringUtils.trim(sequenceObject.getSequence());

                    if (Long.parseLong(today) > Long.parseLong(seqDay)) {

                        sequenceObject.setToday(today);
                        sequenceObject.setSequence(INIT_INDEX);

                    }
                    else {
                        int bigNumber = Integer.parseInt("1" + sequence) + 1;
                        sequence = new String("" + bigNumber).substring(1);
                        sequenceObject.setSequence(sequence);
                    }
                }
                else {
                    sequenceObject = new SequenceObject();
                    sequenceObject.setSeqID(seqID);
                    sequenceObject.setToday(today);
                    sequenceObject.setSequence(INIT_INDEX);

                    session.save(sequenceObject);
                }

                return
                    StringUtils.trim(sequenceObject.getSeqID()) +
                    StringUtils.trim(sequenceObject.getToday()) +
                    StringUtils.trim(sequenceObject.getSequence());
            }

        });

    }

}
